<?php
class monitor {
	var $CI;
	var $monitor_model;
	public function __construct() {
		$this->CI = & get_instance ();
		$this->CI->load->model ( "media/monitor_model" );
		$this->monitor_model = $this->CI->monitor_model;
	}
	public function save_monitor($monitor) {
		$check_result = $this->check_monitor ( $monitor, false );
		if ($check_result ['result'] == false) {
			return $check_result;
		}
		
		$monitor ['monitor_pic'] = "";
		if (isset ( $_FILES ['monitor_pic1'] ['tmp_name'] ) && is_uploaded_file ( $_FILES ['monitor_pic1'] ['tmp_name'] )) {
			$monitor_pic = $this->save_monitor_pic ( "monitor_pic" );
			if ($monitor_pic ['result'] == false) {
				return $monitor_pic;
			}
			$monitor ['monitor_pic'] = $monitor_pic ['upload_file'];
		}
		
		$monitor_id = $this->monitor_model->save_monitor ( $monitor );
		
		$result ['result'] = true;
		$result ['message'] = "添加媒体监测成功";
		$result ['monitor_id'] = $monitor_id;
		$result ['redirect_url'] = base_url () . "index.php/media_monitor_manage";
		return $result;
	}
	private function get_brand_id($brand_name) {
		$this->CI->load->library ( "sys/brand" );
		$brand_info = $this->CI->brand->get_brand_info_by_name ( $brand_name );
		if ($brand_info) {
			return $brand_info ['brand_id'];
		}
	}
	public function create_media_thumb($img) {
		$config ['image_library'] = 'gd2';
		$config ['source_image'] = $this->CI->config->item ( 'ssi_file_path' ) . $img;
		$config ['create_thumb'] = TRUE;
		$config ['maintain_ratio'] = TRUE;
		$config ['width'] = 200;
		$config ['height'] = 150;
		
		$this->CI->load->library ( 'image_lib', $config );
		$this->CI->image_lib->resize ();
	}
	public function check_monitor($monitor, $check_pic = true) {
		$result ['result'] = true;
		$result ['message'] = "监测信息填写正确";
		if ($monitor ['industry'] == "") {
			$result ['result'] = false;
			$result ['message'] = "请选择监测媒体的品类";
			return $result;
		}
		if ($monitor ['media_id'] == "") {
			$result ['result'] = false;
			$result ['message'] = "请选择监测媒体";
			return $result;
		}
		if ($monitor ['product_name'] == "") {
			$result ['result'] = false;
			$result ['message'] = "请填写产品名称";
			return $result;
		}
		if ($check_pic) {
			if (! isset ( $_FILES ['monitor_pic'] ['tmp_name'] ) || ! is_uploaded_file ( $_FILES ['monitor_pic'] ['tmp_name'] )) {
				$result ['result'] = false;
				$result ['message'] = "请上传监测图片";
				return $result;
			}
		}
		return $result;
	}
	public function save_monitor_pic($input_file_name) {
		$this->CI->load->library ( "tools/ssi_upload_file" );
		return $this->CI->ssi_upload_file->upload_pic ( $input_file_name );
	}
	public function query_monitor_list($user_id, $search) {
		$start = intval ( $this->CI->input->get ( "per_page" ) );
		$limit = $this->CI->config->item ( "page_size" );
		return $this->monitor_model->query_monitor_list ( $user_id, $start, $limit, $search );
	}
	public function query_monitor_num($user_id, $search) {
		return $this->monitor_model->query_monitor_num ( $user_id, $search );
	}
	public function query_monitor($monitor_id) {
		return $this->monitor_model->query_monitor ( $monitor_id );
	}
	public function query_monitor_detail($monitor_id) {
		return $this->monitor_model->query_monitor_detail ( $monitor_id );
	}
	public function update_monitor($monitor, $monitor_id) {
		$check_result = $this->check_monitor ( $monitor, false );
		if ($check_result ['result'] == false) {
			return $check_result;
		}
		
		if (isset ( $_FILES ['monitor_pic1'] ['tmp_name'] ) && is_uploaded_file ( $_FILES ['monitor_pic1'] ['tmp_name'] )) {
			
			$monitor_pic = $this->save_monitor_pic ( "monitor_pic1" );
			if ($monitor_pic ['result'] == false) {
				return $monitor_pic;
			}
			$monitor ['monitor_pic'] = $monitor_pic ['upload_file'];
		}
		$this->monitor_model->update_monitor ( $monitor, $monitor_id );
		// 插入日志
		$result ['result'] = true;
		$result ['message'] = "编辑监测日期成功";
		$result ['redirect_url'] = base_url () . "index.php/media_monitor_manage";
		return $result;
	}
	public function update_monitor_time($monitor, $monitor_id) {
		$this->monitor_model->update_monitor ( $monitor, $monitor_id );
		// 插入日志
		$result ['result'] = true;
		$result ['message'] = "编辑监测媒体成功";
		$result ['redirect_url'] = base_url () . "index.php/media_monitor_manage";
		return $result;
	}
	public function modify_monitor_pic($monitor, $monitor_id) {
		$this->monitor_model->update_monitor ( $monitor, $monitor_id );
	}
	public function delete_monitor($monitor_id) {
		$this->monitor_model->delete_monitor ( $monitor_id );
	}
}